// packageB/member/income/regional-agent/query/query.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: '',
    regionText: '',
    regionValue: {},
    chooseRegionShow: false,
    streetText: '',
    chooseStreetShow: false,
    streetColumns: [],
    list: [],
    areaList: {},
    memberInfo: {}
  },

  /**
   * 显示/隐藏弹窗
   * @param {Object} e 
   */
  popupToggle: function(e) {
    const { text, show } = e.currentTarget.dataset
    this.setData({
      [text]: show
    })
  },

  /**
   * 地区选中确认
   * @param {Object} e 
   */
  areaConfirm: function(e) {
    const { values } = e.detail
    if (!values[0].code) {
      app.tips('请选择地区')
      return
    }
    const areaTextList = []
    values.forEach(item => {
      if (item && item.name) {
        areaTextList.push(item.name)
      }
    })
    this.setData({
      regionValue: {
        province: values[0].code,
        city: values[1] && values[1].code ? values[1].code : '',
        district: values[2] && values[2].code ? values[2].code : '',
        street: ''
      },
      streetText: '',
      regionText: areaTextList.join('/'),
      chooseRegionShow: false
    })
    if(values[2] && values[2].code) {
      this.getStreetList(values[2].code)
    }
  },

  /**
   * 通过县级id获取街道数据
   * @param {String | Number} district_id 县级id
   */
  getStreetList: function(district_id) {
    if(!district_id) return
    this.setData({ streetColumns: [] })
    wx.showLoading({ title: '获取街道...' })
    const _this = this
    app._getNetWork({
      url: app.getNetAddresss('member.memberAddress.street'),
      data: { district_id },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.setData({ streetColumns: resData.data || [] })
        } else {
          app.tips(resData.msg || '街道数据获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 街道选中
   * @param {Object} e 
   */
  streetConfirm: function(e) {
    const { value } = e.detail
    this.setData({
      'regionValue.street': value.id,
      streetText: value.areaname,
      chooseStreetShow: false
    })
  },

  /**
   * 搜索
   */
  search: function() {
    const { province, city, district, street } = this.data.regionValue
    if (!province) {
      app.tips('请选择地区！')
      return
    }
    const _this = this
    this.setData({ list: [] })
    wx.showLoading({ title: '加载中...' })
    app._getNetWork({
      url: app.getNetAddresss('plugin.area-dividend.api.area-dividend.getRegionAgent'),
      data: { province, city, district, street },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          if (resData.data.length) {
            const list = resData.data.map(item => {
              return {
                id: item.member_id,
                nickname: item.nickname,
                avatar: item.avatar,
                region: [item.area[0], item.area[1], item.area[2]].join('>')
              }
            })
            _this.setData({ list })
          }
        } else {
          app.tips(resData.msg || '查询失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 初始化地区数据
   */
  _initAddressInfo: function() {
    const _this = this
    app._getNetWork({
      url: app.getNetAddresss("member.member-address.address"),
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const areaList = {
            province_list: {},
            city_list: {},
            county_list: {}
          }
          resData.data.province.forEach(item => {
            areaList.province_list[item.id] = item.areaname
          })
          resData.data.city.forEach(item => {
            areaList.city_list[item.id] = item.areaname
          })
          resData.data.district.forEach(item => {
            areaList.county_list[item.id] = item.areaname
          })
          _this.setData({
            areaList
          })
        } else {
          app.tips(resData.msg)
        }
      }
    })
  },

  /**
   * 获取用户信息及公共规则
   */
  getMemberInfo: function() {
    const _this = this
    wx.showLoading({
      title: '获取用户信息...',
    })
    app._getNetWork({
      url: app.getNetAddresss('plugin.area-dividend.api.area-dividend.getMemberInfo'),
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const result = resData.data
          result.areaText = result.area.length ? result.area.join('>') : ''
          _this.setData({ memberInfo: result })
        } else {
          app.tips(resData.msg || '用户信息获取失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._initAddressInfo()
    this.getMemberInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})